import Api from "@/api/api-declare";
import { ACCEPT_ORDER_SOURCE, NEAY_BY_ORDER_SORT_TYPE, ORDER_STATUS, SCHEDULE_STATUS } from "../enum/pd-enum";
import type { ApiOrderTypes } from "@/api/api-types/api-order-types";
import locationMgr from "../class/location-mgr";
import PD from "../libs";
import OrderHelp from "../libs/order-help";
import { requestSubscribeMessage } from "../libs/subscribe-msg";
import Store from '@/store';
import voiceMgr from "../class/voiceMgr";
import { SERVER_CODE } from "../enum/web-and-server-code";
import { APP_ROUTES } from '../enum/app-routes';
//获取订单广场订单列表
export function useOnTheWayOrder() {
	const query = ref({
		id: 0,//线路id
	});
	const orders = ref<ApiOrderTypes.OrderDto[]>([]);
	const getOrders = (): Promise<void> => {
		return new Promise((r) => {
			Api.ApiOrder.getOnTheWayOrders(query.value).then(res => {
				//转成统一的结构
				if (res.list && res.list.length) {
					const result: ApiOrderTypes.OrderDto[] = res.list.map(v => {
						const timeTransfer = OrderHelp.Time.orderTimePhp2go({
							start: v.depart_at,
							end: v.depart_at,
							channel: v.channel,
							time_range: v.extend.time_range
						});
						return {
							matching: v.matching,
							depart_time_left: timeTransfer.start,//todo
							arrive_time_right: timeTransfer.end,//todo
							business_id: v.business_id,
							cat_name: '',
							channel: v.channel,
							channel_name: v.extend.channel_name,
							departure: {
								address: v.address_start,
								province: v.extend.location.depart.province,
								city: v.extend.location.depart.city,
								district: v.extend.location.depart.district,
								latitude: Number(v.extend.location.depart.latitude),
								longitude: Number(v.extend.location.depart.longitude),
							},
							destination: {
								address: v.address_end,
								province: v.extend.location.arrive.province,
								city: v.extend.location.arrive.city,
								district: v.extend.location.arrive.district,
								latitude: Number(v.extend.location.arrive.latitude),
								longitude: Number(v.extend.location.arrive.longitude),
							},
							estimate: {
								distance: Number(PD.Filters.Common.numberToFixed(v.extend.distance / 1000, 1)),//转成统一单位km
								duration: Number(v.extend.duration) * 60,//转成统一单位秒
							},
							insurance: false,
							is_first: 0,
							line_id: v.line_id,
							num: v.num,
							order_num: v.order_num,
							pay_status: 0,
							price: {
								cp_fee: 0,
								dist_commission: v.first_commission / 100,
								driver_fee: v.total_fee / 100,
								total_fee: 0
							},
							remark: v.remark,
							status: v.status,
						};
					});
					orders.value = result;
				};
				r();
			});
		});
	};

	return {
		query,
		orders,
		getOrders
	};
};


//附近订单
export function useNearByOrder() {
	//首选项
	const firstOpsQuery = ref<ApiOrderTypes.GetNearByOrderFirstOpsReq>({
		city_id: '',
		area_type: 'depart',
		province_id: ''
	});
	const firstOps = ref<ApiOrderTypes.GetNearByOpsItem[]>([]);
	const getFirstOps = (): Promise<ApiOrderTypes.GetNearByOpsItem[]> => {
		return new Promise((r) => {
			Api.ApiOrder.getNearByOrderFirstOps(firstOpsQuery.value).then(res => {
				firstOps.value = res.option;
				r(res.option);
			});
		});
	};

	//次选项
	const secondOpsQuery = ref<ApiOrderTypes.GetNearByOrderSecondOpsReq>({
		city_id: '',
		area_type: 'depart',
		province_id: '',
		district_id: ''
	});
	const secondOps = ref<ApiOrderTypes.GetNearByOpsItem[]>([]);
	const getSecondOps = (): Promise<ApiOrderTypes.GetNearByOpsItem[]> => {
		return new Promise((r) => {
			Api.ApiOrder.getNearByOrderSecondOps(secondOpsQuery.value).then(res => {
				secondOps.value = res.option;
				r(res.option);
			});
		});
	};


	const nearByOrders = ref<ApiOrderTypes.OrderDto[]>([]);
	const nearByOrderQuery = ref<ApiOrderTypes.GetNearByOrdersReq>({
		depart_province_id: 0,//默认传0,当is_default=1时传省id
		depart_city_id: 0,
		depart_district_id: 0,
		arrive_city_id: 0,
		arrive_district_id: 0,
		is_default: 0,//1:未选中任何选项既默认，0：任意选中
		page: 1,
		page_size: 20,
		sort: NEAY_BY_ORDER_SORT_TYPE.smart
	});
	const getNearByOrders = (): Promise<void> => {
		return new Promise((r) => {
			Api.ApiOrder.getNearByOrders(nearByOrderQuery.value).then(res => {
				nearByOrders.value = [...nearByOrders.value, ...res.list];
				r();
			});
		});
	};

	return {
		firstOpsQuery,
		firstOps,
		getFirstOps,
		secondOpsQuery,
		secondOps,
		getSecondOps,
		nearByOrders,
		nearByOrderQuery,
		getNearByOrders
	};
};


//订单详情
export function useOrderDetail() {
	const orderDetail = ref<ApiOrderTypes.OrderDto>();

	//go版本 @时瑞
	const getOrderDetailGoVersion = (p: { order_num: string }): Promise<void> => {
		return new Promise((r) => {
			Api.ApiOrder.getOrderDetailGoVersion({
				order_num: p.order_num
			}).then(res => {
				//转成标准dto结构
				orderDetail.value = {
					depart_time_left: res.schedule_start * 1000,//开始时间戳，13位
					arrive_time_right: res.schedule_end * 1000,//结束时间戳，13位
					business_id: res.business_type,//用车类型
					cancel_info: {
						cancel_reason: '',
						cancel_type: 1
					},
					cat_name: res.car_model_cat_name,//舒适5座 经济5座等
					channel: res.channel, //渠道
					channel_name: res.channel_name,//渠道名称
					departure: {
						address: res.depart_address,
						city: '',
						province: '',
						district: '',
						latitude: Number(res.depart_latitude),
						longitude: Number(res.depart_longitude)
					},//起点信息
					destination: {
						address: res.arrive_address,
						city: '',
						province: '',
						district: '',
						latitude: Number(res.arrive_latitude),
						longitude: Number(res.arrive_longitude)
					},//终点信息
					estimate: {
						distance: res.distance,
						duration: res.duration
					},
					line_id: res.line_id,//线路id
					num: res.num,//用车人数
					order_num: res.order_outers_num,//订单号
					price: {
						cp_fee: 0,
						dist_commission: 0,//分销佣金
						driver_fee: Number(res.driver_fee),//司机到手价
						total_fee: 0,//订单总价
					},
					pay_status: 0,
					remark: res.remark,//备注
					status: res.status,
				};
				r();
			});
		});
	};

	//根据订单号获取订单信息：php版本，无需登录
	const getOrderDetailOuter = (params: ApiOrderTypes.GetOrderDetailOuterReq): Promise<void> => {
		return new Promise((r) => {
			Api.ApiOrder.getOrderDetailOuter(params).then(res => {
				//转成标准dto结构
				const timeTransfer = OrderHelp.Time.orderTimePhp2go({
					start: res.depart_at,
					end: res.depart_at,
					channel: res.channel_id,
					time_range: res.extend.time_range
				});
				orderDetail.value = {
					depart_time_left: timeTransfer.start,//开始时间戳，13位
					arrive_time_right: timeTransfer.end,//结束时间戳，13位
					business_id: res.business_id,//用车类型
					cancel_info: {
						cancel_reason: '',
						cancel_type: 1
					},
					cat_name: res.car_model_cat.name,//舒适5座 经济5座等
					channel: res.channel_id, //渠道
					channel_name: res.channel_name,//渠道名称
					departure: {
						address: res.address_start.startsWith(res.extend.location.depart.city) ? res.address_start.replace(res.extend.location.depart.city, '') : res.address_start,
						city: res.extend.location.depart.city,
						province: res.extend.location.depart.province,
						district: res.extend.location.depart.district,
						latitude: Number(res.extend.location.depart.latitude),
						longitude: Number(res.extend.location.depart.longitude)
					},//起点信息
					destination: {
						address: res.address_end.startsWith(res.extend.location.arrive.city) ? res.address_end.replace(res.extend.location.arrive.city, '') : res.address_end,
						city: res.extend.location.arrive.city,
						province: res.extend.location.arrive.province,
						district: res.extend.location.arrive.district,
						latitude: Number(res.extend.location.arrive.latitude),
						longitude: Number(res.extend.location.arrive.longitude)
					},//终点信息
					estimate: {
						distance: 0,
						duration: 0
					},
					line_id: res.line_id,//线路id
					num: res.num,//用车人数
					order_num: res.order_num,//订单号
					price: {
						cp_fee: 0,
						dist_commission: 0,//分销佣金
						driver_fee: Number((res.driver_fee / 100).toFixed(2)),//司机到手价
						total_fee: 0,//订单总价
					},
					pay_status: 0,
					remark: res.remark,//备注
					status: res.status,
					outer_order_num: res.outer_order_num
				};
				r();
			});
		});
	};
	
	/**
	 * 转单订单详情
	 */
	const saleOrderDetail = (order_num : string) : Promise<ApiOrderTypes.OrderDto> => {
		return new Promise(async (r,j) => {
			Api.ApiOrder.getSaleOrderInfo({
				order_num: order_num,
			}).then(res => {
				r(res);
			}).catch(() => {
				j();
			});
		});
	};
	return {
		orderDetail,
		getOrderDetailGoVersion,
		getOrderDetailOuter,
		saleOrderDetail
	};
};


/**
 * 未完成订单订单结构转成基准结构辅助方法
 */
export function unfinishOrder2BasicOrderDto (order : ApiOrderTypes.GetUnfinishOrderItem) : ApiOrderTypes.OrderDto {
	//转成统一的数据结构
	const timeTransfer = OrderHelp.Time.orderTimePhp2go({
		start: order.schedule_start,
		end: order.schedule_start,
		channel: order.channel,
		time_range: order.extend.time_range
	});
	return {
		batch_num: order.batch_num,
		order_num: order.order_num,
		outer_order_num : order.outer_order_num,
		depart_time_left: timeTransfer.start,//todo
		arrive_time_right: timeTransfer.end,//todo
		business_id: order.business_id,
		cat_name: order.car_model_cat?.name,
		channel: order.channel,
		create_type : order.create_type,
		channel_name: order.extend.channel_name,
		channel_type : order.channel_type,
		departure: {
			address: order.address_start,
			province: order.extend.location.depart.province,
			city: order.extend.location.depart.city,
			district: order.extend.location.depart.district,
			latitude: Number(order.extend.location.depart.latitude),
			longitude: Number(order.extend.location.depart.longitude),
		},
		destination: {
			address: order.address_end,
			province: order.extend.location.arrive.province,
			city: order.extend.location.arrive.city,
			district: order.extend.location.arrive.district,
			latitude: Number(order.extend.location.arrive.latitude),
			longitude: Number(order.extend.location.arrive.longitude),
		},
		estimate: {
			distance: Number(PD.Filters.Common.numberToFixed(order.extend.distance / 1000, 1)),//转成统一单位km
			duration: order.extend.duration
		},
		insurance: false,
		is_first: 0,
		line_id: order.line_id,
		num: order.num,
		price: {
			cp_fee: 0,
			dist_commission: 0,
			driver_fee: Number(PD.Filters.Common.numberToFixed(order.driver_fee / 100)),
			total_fee: Number(PD.Filters.Common.numberToFixed(order.total_fee / 100)),
			pay_fee : Number(PD.Filters.Common.numberToFixed(order.pay_fee / 100))
		},
		remark: order.remark,
		status: order.status,
		pay_status: order.pay_status,
		page_obj: order.page_obj,
		extend: order.extend,
		pay_node_is_offline_pay: order.pay_node_is_offline_pay,
		client_show_mobile: order.client_show_mobile === '尾号' ? '' : order.client_show_mobile,
		contact_mobile: order.contact_mobile,
		receive_source: order.receive_source,
		schedule_id: order.schedule_id,
		driver_pay_for_passenger : order.driver_pay_for_passenger
	};
};
/**
 * 获取司机未完成订单
 */
export function useDriverUnfinishedOrder() {
	const unfinishOrders = ref<ApiOrderTypes.OrderDto[]>([]);
	const unfinishSchedules = ref<ApiOrderTypes.GetUnfinishScheduleItem[]>([]);
	const getUnfinishOrders = (): Promise<void> => {
		return new Promise((r) => {
			Api.ApiOrder.getDriverUnfinished().then(res => {
				if (res.order && res.order.length) {//未完成订单
					const v: ApiOrderTypes.OrderDto[] = res.order.map(v => {
						v.extend.duration = Number(v.extend.duration) * 60;
						return unfinishOrder2BasicOrderDto(v);
					});
					unfinishOrders.value = v;
				};

				//未完成行程
				if (res.schedule && res.schedule.length) {
					res.schedule = res.schedule.map(v => {
						v.driver_order = v.driver_order.map(t => {
							t.extend.duration = Number(t.extend.duration) * 60;
							return unfinishOrder2BasicOrderDto(t);
						});
						return v;
					});
					unfinishSchedules.value = res.schedule;
				};
				r();
			});
		});
	};
	return {
		unfinishOrders,
		unfinishSchedules,
		getUnfinishOrders
	};
};



/**
 * 订单操作相关
 */
export function useOrderCtrl() {
	/**
	 * 接单：订单广场/附近订单接单
	 * @param {ApiOrderTypes.OrderDto} order 订单实体
	 * @param {ACCEPT_ORDER_SOURCE} source 接单来源
	 * @returns 
	 */
	const acceptOrderFromSquare = (order: ApiOrderTypes.OrderDto, source: ACCEPT_ORDER_SOURCE, queue_id ?: number): Promise<ApiOrderTypes.AcceptFromSquareRes> => {
		return new Promise((r, j) => {
			requestSubscribeMessage(PD.AppDeclare.getCurrentApp()?.tmpIds.newOrder).then(async () => {
				const { latitude, longitude } = await locationMgr.getLocation();
				Api.ApiOrder.acceptFromSquare({
					outer_order_num: order.order_num,
					source,
					latitude,
					longitude,
					line_id: order.line_id,
					queue_id : queue_id ? queue_id : 0
				}).then(async res => {
					PD.WxHelp.wxReport(source === ACCEPT_ORDER_SOURCE.square ? 'carpooling_routeorder_get' : 'carpooling_nearbyorder_get');
					Api.ApiJiaoGuan.DDCG(order.order_num);
					//如果有未完成行程，尝试将该订单添加到行程内
					if (res.unschedule.length) {
						await addOrder2Schedule(res.order_num, res.unschedule[0].id, res.bactch_num);
					};
					//播放语音
					voiceMgr.play('receiveOrder', order.channel);
					r(res);
				}).catch(err => {
					PD.UniHelp.modal(err.msg);
					j(err);
				});
			});
		});
	};

	/**
	 * 接单-车队发起的订单
	 */
	const acceptOrderFromCp = (order: ApiOrderTypes.OrderDto, source: ACCEPT_ORDER_SOURCE): Promise<ApiOrderTypes.AcceptFromSquareRes> => {
		return new Promise(async (r, j) => {
			requestSubscribeMessage(PD.AppDeclare.getCurrentApp()?.tmpIds.newOrder).then(async () => {
				const { latitude, longitude } = await locationMgr.getLocation();
				Api.ApiOrder.acceptFromCp({
					order_num: order.order_num,
					source,
					latitude,
					longitude,
					line_id: order.line_id
				}).then(async res => {
					PD.WxHelp.wxReport(source === ACCEPT_ORDER_SOURCE.square ? 'carpooling_routeorder_get' : 'carpooling_nearbyorder_get');
					//如果有未完成行程，尝试将该订单添加到行程内
					if (res.unschedule.length) {
						await addOrder2Schedule(res.order_num, res.unschedule[0].id, res.bactch_num);
					};
					Api.ApiJiaoGuan.DDCG(order.order_num);
					//播放语音
					voiceMgr.play('receiveOrder', order.channel);
					r(res);
				}).catch(err => {
					//saas订单该用户无接单权限
					if(err.code === SERVER_CODE.saasNotTeam){
						PD.UniHelp.navTo(`${APP_ROUTES.joinSaasChannel}?channel_id=${Store.inviter().saasChannelInfo.channel_id}`);
						return;
					};
					//saas订单车辆信息不完善
					if(err.code === SERVER_CODE.VehicleInformationIsIncomplete){
						PD.UniHelp.navTo(`${APP_ROUTES.improveVehicleInformation}?path=${APP_ROUTES.appointment}`);
						return;
					};
					PD.UniHelp.modal(err.msg);
					j(err);
				});
			});
		});
	};

	/**
	 * 取消订单
	 * @param order 订单实体
	 * @param type 取消类型：拒绝/关闭
	 * @param reason 取消订单原因文字
	 */
	const closeOrder = (order: ApiOrderTypes.OrderDto, type: 'reject' | 'close', reason: string): Promise<void> => {
		return new Promise((r) => {
			const api = type === 'reject' ? Api.ApiOrder.rejectOrder({
				order_num : order.order_num as string,
				reason
			}) : Api.ApiOrder.cancelOrder({
				order_num : order.batch_num as string,
				reason
			});
			api.then(() => {
				r();
			});
		});
	};

	/**
	 * 尝试添加某个订单到行程中
	 */
	const addOrder2Schedule = (order_num : string, schedule_id : number, batch_num : string) : Promise<void> => {
		return new Promise((r) => {
			Api.ApiOrder.orderEnableJoinSchedule({
				order_num,
				schedule_id,
			}).then(res => {
				if (res.is_add) {
					Api.ApiOrder.addOrder2Schedule({
						orders : [{order_num, batch_num}],
						schedule_id
					}).finally(() => {
						r();
					});
					return;
				};
				r();
			}).catch(() => {
				r();
			});
		});
	};

	/**
	 * 开始服务订单-对应订单卡片中的【开始行程】按钮操作
	 */
	const startOrderService = (order: ApiOrderTypes.OrderDto): Promise<{ schedule_id: number }> => {
		return new Promise(async (r) => {
			Api.ApiOrder.recomdOrder2Schedule({
				order_num: order.batch_num as string,
			}).then(res => {
				return Api.ApiOrder.startSchedule({
					orders: res.map(v => {
						return {
							order_num: v.batch_num
						};
					})
				});
			}).then(res => {
				r({
					schedule_id: res.schedule_id
				});
			});
		});
	};
	
	/**
	 * 订单微信支付 
	 */
	const orderWxPay = (order_num : string,showErrorModal = true) : Promise<ApiOrderTypes.PayWechatMiniRes> => {
		return new Promise(async(r,j) => {
			Api.ApiOrder.payWechatMini({order_num,openid:Store.user().openid},showErrorModal).then(res => {
				r(res.data);
			}).catch((err) => {
				j(err);
			});
		});
	};

	/**
	 * 联系乘客
	 */
	const contactPassenger = (order : ApiOrderTypes.OrderDto) => {
		PD.UniHelp.loading();
		Api.ApiOrder.callPassengerLog({
			order_num : order.batch_num as string
		}).then(() => {
			return Api.ApiOrder.checkVirtualMobile({
				order_num : order.order_num
			});
		}).then(res=> {
			const mobile = res.virtual_mobile || order?.contact_mobile;
			if (!mobile) {
				PD.UniHelp.modal('未查询到乘客有效联系电话，请联系客服处理');
				return;
			};
			PD.UniHelp.makePhoneCall(String(mobile));
		}).finally(() => PD.UniHelp.loading(false));
	};

	return {
		acceptOrderFromSquare,
		closeOrder,
		startOrderService,
		acceptOrderFromCp,
		orderWxPay,
		contactPassenger
	};
};



/**
 * 行程相关
 */
export function useSchedule () {
	const scheduleInfo = ref<ApiOrderTypes.ScheduleInfo>();
	const getScheduleInfo = (id : number) : Promise<void> => {
		return new Promise(async (r) => {
			PD.UniHelp.loading();
			Api.ApiOrder.getScheduleInfo({id}).then(async res => {
				if (res.orders.length) {
					scheduleInfo.value = {
						...res,
						orders : await schedulePlan(
							res.id,
							res.status,
							res.orders.map(v => unfinishOrder2BasicOrderDto(v))
						)
					};
				};
				r();
			}).finally(() => PD.UniHelp.loading(false));
		});
	};

	//行程规划
	const schedulePlan = (schedule_id : number, status : SCHEDULE_STATUS, orders : ApiOrderTypes.OrderDto[]) : Promise<ApiOrderTypes.OrderDto[]> => {
		return new Promise(async (r) => {
			if ([SCHEDULE_STATUS.unstart, SCHEDULE_STATUS.complete].includes(status) || !orders.length) {
				return r(orders);
			};
			const l = await locationMgr.getLocation();
			Api.ApiOrder.scheduleOrdersPlan({
				schedule_id,
				type : 'distance',
				location : {
					latitude : l.latitude,
					longitude : l.longitude
				}
			}).then(res => {
				const newOrders : ApiOrderTypes.OrderDto[] = [];
				//去接乘客时，以depart_sort排序，送乘客时，以arrive_sort排序
				const sortTarget = status < SCHEDULE_STATUS.during ? res.depart_sort : res.arrive_sort;
				sortTarget.map(v => {
					const order = orders.find(vv => vv.batch_num === v!.batch_num);
					order && newOrders.push(order);
				});
				//在送达目的地时，已送达的订单不会参与行程规划，所以这里要把不在行程规划内的订单放回orders内，保证所有订单都显示出来
				if (newOrders.length !== orders.length) {
					orders.map(v => {
						if (!sortTarget.some(t => t.batch_num === v.batch_num)) {
							newOrders.push(v);
						};
					});
				};
				//接到乘客前的订单，都采用规划，后面的状态，根据订单状态进行排序
				let v = newOrders.filter(v => v.status <= ORDER_STATUS.arriveDepart);
				if (newOrders.some(v => v.status > ORDER_STATUS.arriveDepart)) {
					v = [...v, ...newOrders.filter(v => v.status > ORDER_STATUS.arriveDepart).sort((a,b) => a.status - b.status)];
				};
				r(v);
			}).catch(() => {
				r(orders);
			});
		});
	};

	return {
		scheduleInfo,
		getScheduleInfo
	};
};

/**
 * 接送机相关
 */
export function useAirportTransfer(){
	const airportQuery = ref({
		carpool : 1,
		page : 1,
		size : 10
	});
	const airportList = ref<ApiOrderTypes.OrderDto[]>([]);
	const getAirportList = () : Promise<void> => {
		return new Promise((r) => {
			Api.ApiOrder.getAirportTransferList(airportQuery.value).then(res => {
				if(res && res.length){
					const result : ApiOrderTypes.OrderDto[] = res.map(v => {
						const timeTransfer = OrderHelp.Time.orderTimePhp2go({
							start: v.depart_at,
							end: v.depart_at,
							channel: v.channel,
							time_range: v.extend.time_range
						});
						const flightTime = OrderHelp.Time.orderFlightTime({
							type_id : Number(v.type_id),
							...v.extend.flightInfo
						});
						return {
							depart_time_left: timeTransfer.start,//todo
							arrive_time_right: timeTransfer.end,//todo
							business_id: v.business_id,
							cat_name: '',
							channel: v.channel,
							channel_name: v.extend.channel_name,
							departure: {
								address: v.address_start,
								province: v.extend.location.depart.province,
								city: v.extend.location.depart.city,
								district: v.extend.location.depart.district,
								latitude: Number(v.extend.location.depart.latitude),
								longitude: Number(v.extend.location.depart.longitude),
							},
							destination: {
								address: v.address_end,
								province: v.extend.location.arrive.province,
								city: v.extend.location.arrive.city,
								district: v.extend.location.arrive.district,
								latitude: Number(v.extend.location.arrive.latitude),
								longitude: Number(v.extend.location.arrive.longitude),
							},
							estimate: {
								distance: Number(PD.Filters.Common.numberToFixed(v.extend.distance / 1000, 1)),//转成统一单位km
								duration: v.extend.duration
							},
							insurance: false,
							is_first: 0,
							line_id: v.line_id,
							num: v.num,
							order_num: v.order_num,
							pay_status: 0,
							price: {
								cp_fee: 0,
								dist_commission: v.first_commission / 100,
								driver_fee: v.total_fee / 100,
								total_fee: 0
							},
							remark: v.remark,
							status: v.status,
							flight_info : {
								type_id : Number(v.type_id),
								flight_number : v.extend.flightInfo?.flightNumber,
								expect_time_str : flightTime.expectTimeStr,
								delay_time : flightTime.delayTime
							}
						};
					});
					airportList.value = [...airportList.value,...result];
				};
				r();
			});
		});
	};
	return {
		airportQuery,
		airportList,
		getAirportList
	};
};